package com.wiiss.ams.cron.task.action;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.wiiss.ams.cron.Cron;

public abstract class CronAction implements Runnable {

    private static final Log log = LogFactory.get();

    protected StringBuilder note = new StringBuilder(); // 用于记录执行相关的日志

    protected String name;

    protected Cron cron;

    protected CronAction(String name) {
        super();
        this.name = name;
    }

    public void setCron(Cron cron) {
        this.cron = cron;
    }

    @Override
    public void run() {
        note.setLength(0); // clear
        log.info("action {} starts", name);
        long nt = System.nanoTime();
        try {
            execute();
            log.info("action {} ends, duration: {}ms, note: {}", name, (System.nanoTime() - nt) / 1000_000, note.toString());
        } catch (Exception e) {
            log.error(e, "action {} fails, duration: {}ms", name, (System.nanoTime() - nt) / 1000_000);
        }
    }

    public abstract void execute();

}
